const qiniu = require('qiniu');
var express = require('express');
var router = express.Router();

// 获取七牛云token  
router.get('/qiniutoken', async (ctx) => {
  try {
    const mac = new qiniu.auth.digest.Mac(process.env.VUE_APP_QINIU_ACCESS_KEY, process.env.VUE_APP_QINIU_SECRET_KEY);
    const options = {
      scope: process.env.VUE_APP_QINIU_SCOPE   // 提取配置信息  
    };
    const putPolicy = new qiniu.rs.PutPolicy(options);
    const uploadToken = putPolicy.uploadToken(mac);

    ctx.body = {
      code: '0',
      success: 'true',
      message: '',
      data: {
        uploadToken
      }
    };
  } catch (error) {
    ctx.body = {
      code: '1',
      success: 'false',
      message: error.message,
      data: null
    };
  }
});